package com.wholesale.beans;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

public class CollectGoods {

	private int recId ;
	private int userId ;
	private int goodsId ;
	private Date addTime ;
	private boolean isAttention ;
	private GoodsInfo goodsInfo ;
	private JSONObject jsonObject;
	public int getRecId() {
	
		return recId;
	}
	public void setRecId(int recId) {
		this.recId = recId;
	}
	public int getUserId() {
		
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public int getGoodsId() {
		
	
		return goodsId;
	}
	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}
	public Date getAddTime() {
		
	
		return addTime;
	}
	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}
	public boolean isAttention() {
		
		return isAttention;
	}
	public void setAttention(boolean isAttention) {
		this.isAttention = isAttention;
	}
	public GoodsInfo getGoodsInfo() {
		
		return goodsInfo;
	}
	public void setGoodsInfo(GoodsInfo goodsInfo) {
		this.goodsInfo = goodsInfo;
	}
	
	public String toJsonString(int opType){
		JSONObject jsonObject = new JSONObject();
		try {
			if(opType == 2){
				jsonObject.put("recId", recId);
				jsonObject.put("userId", userId);
			}else if(opType == 0){
				jsonObject.put("userId", userId);
				jsonObject.put("goodsId", goodsId);
			}else{
				jsonObject.put("recId", recId);
				jsonObject.put("userId", userId);
				jsonObject.put("goodsId", goodsId);
				jsonObject.put("addTime", addTime);
				jsonObject.put("isAttention", isAttention);
				if(goodsInfo != null ){
					jsonObject.put("goodsInfo", goodsInfo.toJsonString());
				}else{
					jsonObject.put("goodsInfo", "[]");
				}
			}
		} catch (Exception e) {
		}
		return jsonObject.toString();
	}
	
	public void jsonAnalytic(String jsonString){
		//JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(jsonString);
			jsonAnalytic(jsonObject);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void jsonAnalytic(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
		
			try {
				recId = jsonObject.getInt("recId");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				userId = jsonObject.getInt("userId");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				goodsId = jsonObject.getInt("goodsId");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			addTime = new Date();
			try {
				addTime.setTime(addTime.parse(jsonObject.getString("addTime")));
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
			try {
				isAttention = jsonObject.getBoolean("isAttention");
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
			goodsInfo = new GoodsInfo();
			try {
				goodsInfo.jsonAnalytic(jsonObject.getString("goodsInfo"));
			} catch (JSONException e) {
				
				e.printStackTrace();
			}
		
	}
}
